package com.qf.ssm.service.impl;

import com.qf.ssm.dto.ResultCode;
import com.qf.ssm.exceptions.AddErrorException;
import com.qf.ssm.exceptions.DeleteErrorException;
import com.qf.ssm.exceptions.UpdateErrorException;
import com.qf.ssm.mapper.UserRoleMapper;
import com.qf.ssm.pojo.CheckStatus;
import com.qf.ssm.pojo.UserRole;
import com.qf.ssm.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
@Service
public class UserRoleServiceImpl implements UserRoleService {

    private UserRoleMapper userRoleMapper;

    @Autowired
    public void setUserRoleMapper(UserRoleMapper userRoleMapper) {
        this.userRoleMapper = userRoleMapper;
    }

    @Override
    public void addUserRole(UserRole userRole) {
        if(userRole.isNull(CheckStatus.ADD)){
            throw new AddErrorException("数据不完整，请检查数据", ResultCode.DATA_NOT_ALLOW_NUL);
        }
          userRoleMapper.addUserRole(userRole);
    }

    @Override
    public void updateUserRole(UserRole userRole) {
        if(userRole.isNull(CheckStatus.UPDATE)){
            throw new UpdateErrorException("更新数据为空",ResultCode.DATA_NOT_ALLOW_NUL);
        }
        userRoleMapper.updateUserRole(userRole);
    }

    @Override
    public void deleteUserRoleByIds(Long[] ids) {
        if(ids==null||ids.length==0){
            throw new DeleteErrorException("删除必须传递id",ResultCode.DATA_NOT_ALLOW_NUL);
        }
        userRoleMapper.deleteUserRoleByIds(Arrays.asList(ids));
    }

    @Override
    public List<UserRole> findAllUserRole() {
        return userRoleMapper.findAllUserRole();
    }
}
